<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){
        if(IS_POST){
            $this->login();
            exit;
        }

        if(cookie('adminId')){
            header("location: ".U('Index/index'));
        }

        $this->display();
    }

    private function login(){
        $user=I('post.user');
        $pwd =I('post.pwd');
        $time=I('post.cookie');
        $db=M('User');
        $where['account']=$user;
        $where['pwd']=MD5($pwd);
		
        if($db->where(" account='".$user."'")->find()){
                //echo $db->getLastSql();
            if($db->where(" account='".$user."' and password=".$pwd)->find()){
                //将用户名加密到cookie
                //cookie("adminId", get_authcode($user,'','www.MapleLeaf.com'), $time);
                cookie("adminId", get_authcode($user,'','www.MapleLeaf.com'), $time);
                //echo $db->getLastSql();
                $this->success('登陆成功',U('Index/index'));
            }else{
                $this->error('密码错误');
            }
        }else{
            $this->error('用户名不存在');
        }
    }

	//安全退出系统
    public function out(){
        cookie('adminId', null);
        session('User',null);
        header("location: " . U('Index/index'));
        exit;
    }
}